/***********************
*DESC:枫叶报警主机配置
*Author:wff 
*Date:2015-07-30
*
************************/
function LoadAlarmsIdSelect(curralarmid)
{
	$("#Selectalarm").empty();
	var selectedValue;	
	var length = CurrAlarmElemMap.size();
	
	for ( var i = 0; i < length; i++) {
		if(i == 0)
			selectedValue = CurrAlarmElemMap.getKey(i);
			
		$("#Selectalarm").append("<option value="+CurrAlarmElemMap.getKey(i)+">"+CurrAlarmElemMap.getValue(i)+"</option>"); 
	}
	if(curralarmid == '' || curralarmid == null || curralarmid == undefined)
		curralarmid = selectedValue;
	$("#Selectalarm").val(curralarmid);
	$("#Selectalarm option[value='" + curralarmid + "']").attr("selected","selected");
	return true;
}
function InitAlarmsDiv(){
	
	LoadAlarmsIdSelect(curralarmid);
	InitAlarmAreaListTable();
	
}
function GetAlarmAreaTableResult(gridId)
{
	var data=[];
	data = $.fn.tables.GetAlarmAreaTableResult(gridId);
	return data;
}
function GetCurrAlarmAreaData(curralarmid)
{
	var isHave = false;
		var ers = [];
		var tbResult = new Array;
	$.each(CurrAlarmsArry,function(index,item){
			
		if(item.id == curralarmid){
			
			if(item.hasOwnProperty("area")){
			//if(item.area != undefined && item.area != null ){
					
					var NewAreas = item.area;
					if(item.area == 0){
						
						return false;
					}else{
						if(typeof(NewAreas) == "object" && NewAreas.constructor == Array){				
							ers=item.area;						
							
						}
						else{
							ers = [];
							ers.push(item.area);
						}
						return false;
					}
				
			}
			else{
				
						
				return false;
			}
		}
	})
	
	return ers;
	
}
function GetCurrAreaZoneData(curralarmid,curralarmAreaid)
{
		var isHave = false;
		var ers;
		var tbResult = new Array;
	$.each(CurrAlarmsArry,function(index,item){
			
			if(item.id == curralarmid){
				var NewAreas = item.area;
				if(typeof(NewAreas) == "object" && NewAreas.constructor == Array){				
					$.each(item.area,function(n,value){
						
						var json = value;
						
						if(value.id == curralarmAreaid){
							if(value.hasOwnProperty("zone")){
								if(value.zone == 0){
										console.log("meiyI");
								}else{
										isFind = true;
										isHave = true;
										var NewZones = value.zone;
										if(typeof(NewZones) == "object" && NewZones.constructor == Array){
										
											ers=value.zone;
											return false;
										}else{
											ers = [];
											ers.push(NewZones);
										}
								}
							}
						}
						
					})
				}else{
					if(NewAreas.id == curralarmAreaid){
							if(NewAreas.hasOwnProperty("zone")){
								if(NewAreas.zone == 0){
										console.log("meiyI");
								}else{
										isFind = true;
										isHave = true;
										var NewZones = NewAreas.zone;
										if(typeof(NewZones) == "object" && NewZones.constructor == Array){
										
											ers=NewAreas.zone;
											
										}else{
											ers = [];
											ers.push(NewZones);
										}
								}
							}
						}
				}
				
				return false;
				
			}
			if(isHave == true)
				return false;
	})
	if(isHave == false)
		return false;
	else
		return ers;
}

function InitCurrAreaZoneTableData(curralarmAreaid, zonearry)
{	
	
	$.fn.tables.bindObjData('AlarmAreaZoneAddtable', eval(zonearry));
}
function InitZonesForm()
{
		var trHtml = $("#AlarmAreaZoneAddtable").find("tr").slice(1).first().html();
		$("#AlarmAreaZoneAddtable").find("tbody").find("tr").remove();
		$("#AlarmAreaZoneAddtable").append("<tr>" + trHtml + "</tr>");
		AlarmZoneidint ++;
		$("#AlarmAreaZoneAddtable #id").val(AlarmZoneidint);
}
function GetZoneFrom(curralarmAreaid)
{
	
	var zonearry = GetCurrAreaZoneData(curralarmid,curralarmAreaid);
	if(zonearry != false)
		InitCurrAreaZoneTableData(curralarmAreaid, zonearry);
	else{
		InitZonesForm();
	}
	$('#AlarmAreaZoneAddModal .input-small').removeClass("error");
	$('#AlarmAreaZoneAddModal .text-error').remove();
	$("#AlarmAreaZoneAddModal_addbtn").attr("onclick","AddZonesAction('"+curralarmid+"','"+curralarmAreaid+"');");
	$('#AlarmAreaZoneAddModal').modal('show'); 
}
function AddZonesAction(curralarmid, curralarmAreaid)
{
	var isHave = false;
	var Tab = GetAlarmAreaTableResult('AlarmAreaZoneAddtable');
	if(Tab != 0)
	{
		var ispass = validateRow('AlarmAreaZoneAddtable');
			if(!ispass){
				$.formSmallInit();
				
				return;
			}
	}
	$.each(CurrAlarmsArry,function(index,item){
			
			if(item.id == curralarmid){
				
				var NewAreas = item.area;
				if(typeof(NewAreas) == "object" && NewAreas.constructor == Array){
					$.each(item.area,function(n,value){
						if(value.id == curralarmAreaid){						
										
							isFind = true;
							isHave = true;				
							item.area.splice(n, 1);						
							item.area.push({"id": curralarmAreaid,"name": value.name,"zone": GetAlarmAreaTableResult('AlarmAreaZoneAddtable')});						
							return false;
						}
					})
				}else{
					if(NewAreas.id == curralarmAreaid){						
										
							isFind = true;
							isHave = true;
							var news = [];
							news.push({"id": curralarmAreaid,"name": NewAreas.name,"zone": GetAlarmAreaTableResult('AlarmAreaZoneAddtable')});
							item.area = news;
							
						}
				}
				return false;
			}				
	})
		 	
//console.log("CurrAlarmsArry=" + JSON.stringify(CurrAlarmsArry));
$('#AlarmAreaZoneAddModal').modal('hide');
$('#AlarmAreaListTable').bootstrapTable('load',{data:GetCurrAlarmAreaData(curralarmid)});
	
}
function AddAlarmAreaAction()
{
	
	var ispass = validateRow('AlarmAreaAddtable');
	if(!ispass){
			return;
	}
	var Tab = GetAlarmAreaTableResult('AlarmAreaAddtable');
	
	if(Tab == 0){
		
		return;
	}
	
	 if(CurrAlarmsArry.length != 0){
		 var epid,name,type;
		 
		$.each(CurrAlarmsArry,function(index,item){
			if(item.id == curralarmid){
				epid = item.epid;
				name = item.name;
				type = item.type;
				if(item.hasOwnProperty("area")){
				if(item.area == 0){
					
					CurrAlarmsArry.splice(index, 1);
					if(Tab.length == 1)
						CurrAlarmsArry.push({"id": curralarmid,"epid": epid,"name": name,"type": type,"area": Tab[0]});
					else
						CurrAlarmsArry.push({"id": curralarmid,"epid": epid,"name": name,"type": type,"area": GetAlarmAreaTableResult('AlarmAreaAddtable')});
				}else{
					
					var NewAreas = item.area;
					if(typeof(NewAreas) == "object" && NewAreas.constructor == Array){
						
						if(Tab != 0){
							
								$.each(Tab,function(Tabindex,Tabvalue){
									item.area.push(Tabvalue);				
								})
							
						}
					}
					else{
						
						var newArea = [];
						newArea.push(item.area);
						if(Tab != 0){
							
								$.each(Tab,function(Tabindex,Tabvalue){
									newArea.push(Tabvalue);				
								})
							
						}
						CurrAlarmsArry.splice(index, 1);
						CurrAlarmsArry.push({"id": curralarmid,"epid": epid,"name": name,"type": type,"area": newArea});
					}
				}
					
					return false;
				}
				else{
					
					 CurrAlarmsArry.push({"id": curralarmid,"epid": epid,"name": name,"type": type,"area": GetAlarmAreaTableResult('AlarmAreaAddtable')});
					return false;
				}
			}
		})
		
				
	 }
	 $('#AlarmAreaAddModal').modal('hide');
	 $('#AlarmAreaListTable').bootstrapTable('load',{data:GetCurrAlarmAreaData(curralarmid)});
}
function DelAlarmAreaAction(currDelIds)
{
	//curralarmid
	var Delrules = currDelIds.split(",");	
	$.each(CurrAlarmsArry,function(index,item){
		if(item.id == curralarmid){
			
			for (var i = 0; i < Delrules.length; i++) {
				var curralarmAreaid = Delrules[i];
				
				var NewAreas = item.area;
				if(typeof(NewAreas) == "object" && NewAreas.constructor == Array){
					$.each(item.area,function(n,value){
						if(value.id == curralarmAreaid){																													
							item.area.splice(n, 1);										
							return false;
						}
					})
				}else{
					if(item.area.id == curralarmAreaid){																													
							item.area.splice(0, 1);										
							
					}	
				}
			}
			return false;
		}
	})

	$("#DeleteAllModal_delbtn").removeAttr("onclick");
	$('#DeleteAllModal').modal('hide');
	$('#AlarmAreaListTable').bootstrapTable('load',{data:GetCurrAlarmAreaData(curralarmid)});
	
}
function InitAlarmAreaListTable()
{
	$('#AlarmAreaListTable').bootstrapTable({							
	data: GetCurrAlarmAreaData(curralarmid),
	//height: 405,
	striped: true,
	dataType: "json",
	pagination: true,	
	singleSelect: false,	
	contentType: 'application/json',
	pageSize: 5,
	pageNumber:1,
	pageList: [5,10, 25, 50, 100],
	showRefresh: false,
	showToggle: false,	
	search: false, //不显示 搜索框
	showColumns: false, //不显示下拉框（选择显示的列）
	sidePagination: "client", //服务端请求
	columns: [
		{
                    field: 'state',
                    checkbox: true
         }, 	
		{
			field: 'id',	
			title: '分区ID',	
			width: '10%',		
			align: 'center',	
			valign: 'middle',	
			sortable: false	
		},
		{
			field: 'name',	
			title: '分区名称',	
			width: '20%',		
			align: 'center',	
			valign: 'middle',	
			sortable: false	
		},		
		{
			field: 'zone',	
			title: '防区信息',	
			width: '50%',	
			align: 'center',	
			valign: 'middle',
			formatter: AlarmZoneInfoFormatter,
			sortable: false	
		},
		
		{
			title: '防区管理',
			field: 'operate',	
			width: '20%',	
			align: 'center',	
			valign: 'middle',	
			formatter: ZoneViewFormatter,				
			sortable: false	
		}
	]
	
});
}

function AlarmZoneInfoFormatter(value, row, index) {

            return [JSON.stringify(row.zone)
                    ].join('');
}
function ZoneViewFormatter(value, row, index) {
return '<a class="btn-flat gray" style="text-decoration:none" onclick="GetZoneFrom(\''+row.id+'\')">防区管理</a>';

}